<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Introduction</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter 1. Boost.Convert 2.0">
<link rel="up" href="../index.html" title="Chapter 1. Boost.Convert 2.0">
<link rel="prev" href="../index.html" title="Chapter 1. Boost.Convert 2.0">
<link rel="next" href="getting_started.html" title="Getting Started">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../index.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="getting_started.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_convert.introduction"></a><a class="link" href="introduction.html" title="Introduction">Introduction</a>
</h2></div></div></div>
<p>
      <span class="emphasis"><em>Boost.Convert</em></span> builds on the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span></code>
      experience and takes those type conversion/transformation-related ideas further
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          to be applicable to a wider range of conversion-related deployment scenarios,
        </li>
<li class="listitem">
          to provide a more flexible, configurable and extendible type-conversion
          framework,
        </li>
<li class="listitem">
          to provide generic consistent behavior,
        </li>
<li class="listitem">
          to <span class="emphasis"><em>unify and to uniformly deploy</em></span> various conversion
          facilities <span class="emphasis"><em>through one consistent interface</em></span>.
        </li>
</ul></div>
<p>
      <span class="emphasis"><em>Boost.Convert</em></span> provides new and familiar conversion/transformation-related
      functionality such as:
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          means to deploy types that do not meet the <a href="http://en.cppreference.com/w/cpp/named_req/DefaultConstructible" target="_top"><span class="emphasis"><em>Default
          Constructibility</em></span></a> requirement;
        </li>
<li class="listitem">
          four types of the conversion-failure <span class="emphasis"><em>detection</em></span>:
          <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; ">
<li class="listitem">
                as part of the same process flow -- basic and generic;
              </li>
<li class="listitem">
                as a branched process flow -- exception-based and function-based;
              </li>
</ul></div>
        </li>
<li class="listitem">
          the choice of immediate or delayed exception-throwing and non-throwing
          conversion-failure <span class="emphasis"><em>processing</em></span>;
        </li>
<li class="listitem">
          flexibility of conversion-failure <span class="emphasis"><em>processing</em></span> via:
          <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; ">
<li class="listitem">
                an exception thrown;
              </li>
<li class="listitem">
                the fallback value returned;
              </li>
<li class="listitem">
                the fallback function called;
              </li>
</ul></div>
        </li>
<li class="listitem">
          support for standard algorithms.
        </li>
</ul></div>
<p>
      <span class="emphasis"><em>Boost.Convert</em></span> consists of two components:
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">convert</span><span class="special">()</span></code>
          interface;
        </li>
<li class="listitem">
          an extendible collection of pluggable converters.
        </li>
</ul></div>
<p>
      The <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">convert</span><span class="special">()</span></code>
      interface
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          advertises, provides and ensures certain <span class="emphasis"><em>consistent behavior</em></span>;
        </li>
<li class="listitem">
          unifies and provides <span class="emphasis"><em>one consistent interface</em></span> to various
          conversion facilities.
        </li>
</ul></div>
<p>
      The collection of pluggable converters is independent of the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">convert</span><span class="special">()</span></code> API facade and is designed to be <span class="emphasis"><em>extendible</em></span>
      and extended over time. Currently the following converters are provided:
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span></code>-based,
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">printf</span><span class="special">/</span><span class="identifier">scanf</span></code>-based,
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">strtol</span></code>-inspired,
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">stringstream</span></code>-based,
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span></code>-based.
        </li>
</ul></div>
<p>
      The converters provide new and familiar functionality and demonstrate how existing
      and independent conversion facilities might be incorporated in to the <span class="emphasis"><em>Boost.Convert</em></span>
      framework. For example, the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">stream</span></code>-based
      converter draws on the standard <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">streams</span></code>
      functionality and provides:
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          string-to-type and type-to-string conversions;
        </li>
<li class="listitem">
          formatting support (<code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ios</span></code>-based
          precision, base, upper/lower-case, scientific, etc.);
        </li>
<li class="listitem">
          support for different <code class="computeroutput"><span class="identifier">locale</span></code>s;
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="keyword">char</span></code> and <code class="computeroutput"><span class="keyword">wchar_t</span></code>
          support.
        </li>
</ul></div>
<p>
      The following code demonstrates conversion of an array of integers from their
      textual <span class="emphasis"><em>hexadecimal</em></span> representation. It assigns -1 to those
      which fail to convert:
    </p>
<p>
</p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">array</span><span class="special">&lt;</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*,</span> <span class="number">3</span><span class="special">&gt;</span> <span class="identifier">strs</span> <span class="special">=</span> <span class="special">{{</span> <span class="string">" 5"</span><span class="special">,</span> <span class="string">"0XF"</span><span class="special">,</span> <span class="string">"not an int"</span> <span class="special">}};</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span>           <span class="identifier">ints</span><span class="special">;</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">cstream</span>         <span class="identifier">cnv</span><span class="special">;</span>

<span class="comment">// Configure converter to read hexadecimal, skip (leading) white spaces.</span>
<span class="identifier">cnv</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">hex</span><span class="special">)(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">skipws</span><span class="special">);</span>

<span class="identifier">std</span><span class="special">::</span><span class="identifier">transform</span><span class="special">(</span><span class="identifier">strs</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">strs</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">back_inserter</span><span class="special">(</span><span class="identifier">ints</span><span class="special">),</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">apply</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">cref</span><span class="special">(</span><span class="identifier">cnv</span><span class="special">)).</span><span class="identifier">value_or</span><span class="special">(-</span><span class="number">1</span><span class="special">));</span>

<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">ints</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">3</span><span class="special">);</span> <span class="comment">// Number of values processed.</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">ints</span><span class="special">[</span><span class="number">0</span><span class="special">]</span> <span class="special">==</span>  <span class="number">5</span><span class="special">);</span>    <span class="comment">// " 5"</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">ints</span><span class="special">[</span><span class="number">1</span><span class="special">]</span> <span class="special">==</span> <span class="number">15</span><span class="special">);</span>    <span class="comment">// "0XF"</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">ints</span><span class="special">[</span><span class="number">2</span><span class="special">]</span> <span class="special">==</span> <span class="special">-</span><span class="number">1</span><span class="special">);</span>    <span class="comment">// "not an int"</span>
</pre>
<p>
    </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2009-2020 Vladimir Batov<p>
        Distributed under the Boost Software License, Version 1.0. See copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>.
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../index.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="getting_started.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
